package cn.iocoder.yudao.module.infra.service.label;

import cn.iocoder.yudao.module.infra.constant.RedisKeyConstants;
import cn.iocoder.yudao.module.infra.controller.admin.label.vo.LabelCreateReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.label.vo.LabelPageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.label.vo.LabelUpdateReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.label.LabelDO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.infra.convert.label.LabelConvert;
import cn.iocoder.yudao.module.infra.dal.mysql.label.LabelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 分类管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LabelServiceImpl implements LabelService {

    @Resource
    private LabelMapper labelMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public Integer createLabel(LabelCreateReqVO createReqVO) {
        // 插入
        LabelDO label = LabelConvert.INSTANCE.convert(createReqVO);
        labelMapper.insert(label);
        // 返回
        return label.getId();
    }

    @CacheEvict(value = RedisKeyConstants.QUERY_TEMPLATE, key = "#updateReqVO.id", allEntries = true)
    @Override
    public void updateLabel(LabelUpdateReqVO updateReqVO) {
        // 校验存在
        validateLabelExists(updateReqVO.getId());
        // 更新
        LabelDO updateObj = LabelConvert.INSTANCE.convert(updateReqVO);
        labelMapper.updateById(updateObj);
    }

    @CacheEvict(value = RedisKeyConstants.QUERY_TEMPLATE, key = "#id", allEntries = true)
    @Override
    public void deleteLabel(Integer id) {
        // 校验存在
        validateLabelExists(id);
        // 删除
        labelMapper.deleteById(id);
    }

    private void validateLabelExists(Integer id) {
        if (labelMapper.selectById(id) == null) {
            throw exception(LABEL_NOT_EXISTS);
        }
    }

    @CacheEvict(value = RedisKeyConstants.QUERY_TEMPLATE,key = "#id")
    @Override
    public LabelDO getLabel(Integer id) {
        return labelMapper.selectById(id);
    }

    @Override
    public List<LabelDO> getLabelList(Collection<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return labelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LabelDO> getLabelPage(LabelPageReqVO pageReqVO) {
        return labelMapper.selectPage(pageReqVO);
    }

    @CacheEvict(value = RedisKeyConstants.QUERY_TEMPLATE)
    @Override
    public List<LabelDO> getSimpleLabelList() {
        return labelMapper.selectList();
    }

    @Override
    public List<LabelDO> getType() {
        return labelMapper.selectTypeList();
    }


}
